package com.xiguaji.tiktok.v1.tiktok.music.controller;

import com.xiguaji.tiktok.config.exception.ParamsException;
import com.xiguaji.tiktok.utils.Ret;
import com.xiguaji.tiktok.v1.BaseController;
import com.xiguaji.tiktok.v1.tiktok.music.dto.SearchData;
import com.xiguaji.tiktok.v1.tiktok.music.service.MusicService;
import com.xiguaji.tiktok.v1.tiktok.permission.Limit;
import com.xiguaji.tiktok.v1.tiktok.permission.RequestLimit;
import com.xiguaji.tiktok.v1.user.log.Operation;
import com.xiguaji.tiktok.v1.user.log.ParamsType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(value = "音乐",tags = {"音乐"})
public class MusicController extends BaseController {

    @Autowired
    MusicService musicService;

    @ApiOperation(value = "热门音乐列表")
    @RequestMapping(value = "music",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "热门音乐列表")
    @RequestLimit(value = "MusicAndChallegeLimit",property = "page,pageSize",validateType = Limit.VIEW_DATA_LIMIT)
    public String getPage(String data) throws IOException, ParamsException {
        SearchData searchData = getParam(SearchData.class, URLDecoder.decode(data,"UTF-8"));
        return musicService.getPageEs(searchData,getUser().getId());
    }

    @ApiOperation(value = "热门音乐详情信息")
    @RequestMapping(value = "music/{musicId}",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "热门音乐详情信息",fetchParam = "musicId",paramType = ParamsType.URL)
    public String getMusicDetail(@PathVariable long musicId) throws IOException {
        return Ret.success("SUCCESS",musicService.getMusicDetail(musicId,getUser().getId()));
    }

    @ApiOperation(value = "热门音乐详情趋势")
    @RequestMapping(value = "music/distribution/{musicId}",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "热门音乐详情信息",fetchParam = "musicId",paramType = ParamsType.URL)
    public String getMusicDistribution(@PathVariable long musicId){
        Map<String,Object> result = new HashMap<>();
        result.put("regionDistribution",musicService.getRegionDistribution(musicId));
        result.put("industryDistribution",musicService.getIndustryDistribution(musicId));
        return Ret.success("SUCCESS",result);
    }

    @ApiOperation(value = "热门音乐相关视频")
    @RequestMapping(value = "music/videos/{musicId}",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @Operation(value = "热门音乐相关视频",fetchParam = "musicId",paramType = ParamsType.URL)
    @RequestLimit(value = "MusicAndChallegeLimit",property = "page,pageSize",validateType = Limit.VIEW_DATA_LIMIT)
    public String getMusicVideos(@PathVariable long musicId,String data) throws UnsupportedEncodingException, ParamsException {
        int page = getParam(Integer.class, URLDecoder.decode(data,"UTF-8"), "page");
        int pageSize = getParam(Integer.class,URLDecoder.decode(data,"UTF-8"),"pageSize");
        int sortType = getParam(Integer.class,URLDecoder.decode(data,"UTF-8"),"sortType");
        val awemePageByMusic = musicService.getAwemePageByMusic(musicId, sortType, page, pageSize, getUser().getId());
        return Ret.pageData(awemePageByMusic.getTotalCount(),awemePageByMusic.getList());
    }
}
